﻿using MySql.Data.MySqlClient;
using QLInternet.Connection;
using QLInternet.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;

namespace QLInternet.Source
{
    class KyThuat
    {
        public ObservableCollection<CustomerViewModel> loadDSKHBaoLoi(Connect c)
        {
            ObservableCollection<CustomerViewModel> li = new ObservableCollection<CustomerViewModel>();

            string sql = "select khachhang.makhachhang, hoten, cmnd, diachicaidat, tengoicuoc, ngaybaoloi, ngaysualoi, cachkhacphuc, mabaoloi" + 
            " from baoloi inner join hopdong on baoloi.mahopdong = hopdong.mahopdong " + 
            " inner join khachhang on hopdong.makhachhang = khachhang.makhachhang" + 
            " inner join dangky on dangky.makhachhang = khachhang.makhachhang " + 
            " inner join goicuoc on dangky.magoicuoc = goicuoc.magoicuoc";

            MySqlDataReader re = null;
            if (!c.IsOpenning)
                c.getConnect();
            MySqlCommand cmd = new MySqlCommand(sql, c.con);
            try
            {
                re = cmd.ExecuteReader();
                int i = 0;
                while (re.Read())
                {
                    i++;
                    string d="";
                    try
                    {
                        d = re.GetDateTime(6).ToString("dd-MM-yyyy hh:mm:ss tt");
                    }
                    catch
                    {
                        d = "Chưa khắc phục";
                    }
                    
                        
                    CustomerViewModel cu = new CustomerViewModel
                    {
                        STT = i + "",
                        MaKh = re.GetInt32(0),
                        HoTen = re.GetString(1),
                        CMND = re.GetInt32(2),
                        DiaChiCaiDat = re.GetString(3),
                        TenGoiCuoc = re.GetString(4),
                        NgayBaoLoi = re.GetDateTime(5),
                        
                             NgaySuaLoiString = d,
                        CachKhacPhuc = re.GetString(7),
                        MaBaoLoi = re.GetInt32(8)
                    };
                    li.Add(cu);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("Có lỗi trong quá trình xử lý." + ex.Message , "Lỗi");
            }
            finally
            {
                re.Close();
            }

            return li;
        }

        public bool updateKhacPhucSuCo(Connect c,int ma, string val)
        {
            string d = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
           
            string sql = "update baoloi set cachkhacphuc='" + val+"'" +
                ", ngaysualoi = '" + d+ "' where mabaoloi = " + ma;
            if (!c.IsOpenning)
                c.getConnect();
            MySqlCommand cmd = new MySqlCommand(sql, c.con);
            if (cmd.ExecuteNonQuery() > 0)
                return true;
            return false;
                
        }
    }
}
